<template>
    <div id="box">
      <el-card class="!border-none" shadow="never">
          <el-row>
              <el-col :span="6">
                <span>库存日期：</span>
                <el-date-picker
                  v-model="date"
                  type="date"
                  :disabled-date="disabledDate"
                  placeholder="选择日期"
                  value-format="YYYY-MM-DD"
                />
              </el-col>
              <el-col :span="2" v-if="role_name=='系统管理员'||role_name=='中润管理员'||role_name=='生产调度'">
                  <el-button type="primary" @click="exportExcel">报表导出</el-button>
              </el-col>
          </el-row>
      </el-card>
      <el-card id="scroll-box" class="!border-none mt-4" shadow="never">
        <div class="bltitle">
          {{date}}各分厂药剂库存统计表
        </div>
        <kucun id="stockComponent" ref="stockComponent" :date="date" :ishistory="1" :isup="true" @ischeck="ischeck" v-if="sfsh"/>
        <div v-else>
          <el-empty>
            <template #description>
              <span style="font-size:18px;font-weight: bold;color:red;">该日数据未审核</span>
            </template>
          </el-empty>
        </div>
      </el-card>
    </div>
</template>
<script lang="ts" setup name="stockHistory">
import cache from '@/utils/cache'
import kucun from '@/components/stock/kucun.vue';
import { getStock } from '@/api/report'

import { exportExcelStockApi } from "@/api/check";

import { GetDateStr } from '@/utils/util'
const date = ref(GetDateStr(-1))
const role_name = ref(cache.get('role_name'));

const sfsh = ref(false)
const ischeck = ()=>{
  sfsh.value = false
}

watch(date, async () => {
  sfsh.value = true
})
const uu = ref(cache.get('role_name'))
onMounted(()=>{
  getStock({isup:true,ishistory:1,date:date.value,company:uu.value}).then((res)=>{
    if(res.length!=0){
      sfsh.value = true
    }else{
      sfsh.value = false
    }
  })
})

const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()  - 24 * 60 * 60 * 1000
}

function exportExcel(){
  exportExcelStockApi({
    date:date.value
  }).then((res:any) => {
    console.log(res)
    window.location.href=res.path
  })
}
</script>

<style>
  .el-form .el-form-item{
    margin-bottom: 0;
  }
  .bltitle{
    height: 44px;
    font-size: 20px;
    text-align: center;
    font-weight: bold;
  }
  .location{
    position: fixed;
    top:50%;
    left:230px;
  }
  .list{
    position: fixed;
    text-align: center;
    width: 200px;
    background: #eee;
    top:15%;
    left:230px;
    border-radius: 5px;
    border: 1px solid #aaa;
  }
  .item{
    margin:10px 5px;
  }
</style>